package com.bnzj.core.util;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.util.StringUtils;

public class DateUtils {
	
	private static DateTimeFormatter longFormatter = DateTimeFormatter.ofPattern(FbConstant.DATETIME_FORMAT);
	
	private static DateTimeFormatter longFormatter2 = DateTimeFormatter.ofPattern(FbConstant.DATETIME2_FORMAT);
	
	private static DateTimeFormatter shortFormatter = DateTimeFormatter.ofPattern(FbConstant.DATE_FORMAT);
	
	private static ZoneOffset defaultZoneOffset = OffsetDateTime.now().getOffset();

	public static final String [] parsePatterns = new String[] { FbConstant.DATETIME_FORMAT, "yyyy-MM-dd HH:mm", FbConstant.DATE_FORMAT};
	
	public static LocalDateTime parseLocalDateTime(String dateTime) {
        return LocalDateTime.parse(dateTime, longFormatter);
    }
	
	public static LocalDateTime parseLocalDate(String date) {
        return LocalDate.parse(date, shortFormatter).atTime(0, 0, 0);
    }
	
	public static Date parseDateTime(String dateStr){
		LocalDateTime localDateTime = parseLocalDateTime(dateStr);
		Date date = Date.from(localDateTime.toInstant(defaultZoneOffset));
		return date;
	}
	
	public static Date parseDate2(String dateStr){
		LocalDateTime localDateTime = parseLocalDate(dateStr);
		Date date = Date.from(localDateTime.toInstant(defaultZoneOffset));
		return date;
	}
	
	public static String getNowTimeStr(){
		LocalDateTime localDateTime = LocalDateTime.now(defaultZoneOffset);
		return longFormatter2.format(localDateTime);
	}
	
	public static String formatDate(final Date date, final String pattern) {
		return date == null ? null : DateFormatUtils.format(date, pattern);
	}
	
	public static String formatDate(final Calendar date, final String pattern) {
		return date == null ? null : DateFormatUtils.format(date, pattern);
	}
	
	public static String formatDateTime(final Date date) {
		return formatDate(date, FbConstant.DATETIME_FORMAT);
	}
	
	public static String formatDateTime(final Calendar date) {
		return formatDate(date, FbConstant.DATETIME_FORMAT);
	}

	public static Date parseDate(final String date, String... parsePatterns) {
		if(StringUtils.isEmpty(date)) {
			return null;
		}
		try {
			return org.apache.commons.lang3.time.DateUtils.parseDate(date, parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date parseDate(final String date) {
		return parseDate(date, parsePatterns);
	}
	
	//获得系统当前年月
	public static	String getYM(){
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH )+1;
		return year+"-"+month;
	}
    
    public static String now(){
        return shortFormatter.format(LocalDateTime.now());
    }
    
    public static String getNowTime(){
		return longFormatter.format(LocalDateTime.now());
    }
    
    public static String addDay(Date date, int num){
    	Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    calendar.add(Calendar.DATE, num);
		LocalDateTime tocalDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
	    return shortFormatter.format(tocalDateTime);
    }
	
	public static Date getNextDate(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		// 把日期往后增加一天.整数往后推,负数往前移动
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}
	
	public static Date getNextMonth(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		// 把日期往后增加一个月.整数往后推,负数往前移动
		calendar.add(Calendar.MONTH, 1);
		return calendar.getTime();
	}
}
